package com.tetris.test;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class GraphicsView extends View{

	//constructor
	public GraphicsView(Context context) {
		super(context);
		setFocusable(true);
	}

	//holds tile colors
	Paint tilePaint = new Paint();
	int[] colors = {Color.BLACK, Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.MAGENTA, Color.LTGRAY};

	//values for screen mapping, initialized elsewhere
	int XscreenOffset, YscreenOffset, XtileCount, YtileCount;
	//sets size of tiles
	int tileSize = 30;

	//2D grid holding info for all tiles
	protected Tile[][] grid;

	/**
	 * Holds the paint value for each tile.
	 * @author Student
	 *
	 */
	public class Tile {
		int paint;
		int[] companionX, companionY = new int[4];
		boolean active;
		//constructor
		public Tile(int newpaint, boolean newactive) {
			paint = newpaint;
			active = newactive;
		}
		//setters & getters
		public void setPaint(int newpaint) {
			paint = newpaint;
		}
		public int getPaint() {
			return paint;
		}
		public void setActive(boolean newactive) {
			active = newactive;
		}
		public boolean isActive() {
			return active;
		}
		public void setCompanions() {
			
		}
		/**
		 * Sets both the activity & paint of a pre-existing Tile.
		 * 
		 */
		public void setTile(int newpaint, boolean newactive) {
			paint = newpaint;
			active = newactive;
		}

		/**
		 * A method to move all values of one tile to a different one.
		 * @param destination
		 */
		public void move(Tile destination) {
			destination.setTile(paint, active);
			paint = 0;
			active = false;
			for(int i = 0; i < 4; i++) {
				destination.companionY[i] = companionY[i];
				destination.companionX[i] = companionX[i];
			}
		}
		/**
		 * A method to check whether a tile or other components of its block are directly above a block or not. 
		 * @return
		 */
		public boolean spaceEmpty() {
			int temp = 0;
			for(int i = 0; i < 4; i++) {
				if(grid[companionX[i]][companionY[i] - 1].getPaint() > 0) {
					temp += 1;
					for(int a = 0; a < 4; a++) {
						if(grid[companionX[i]][companionY[i] - 1] == grid[companionX[a]][companionY[a]]) {
							temp -= 1;
						}
					}
				}
			}
			if(temp > 0) {
				return false;
			}
			else {
				return true;
			}
		}
	}

	/**
	 * Sets the paint value of all tiles to 0 (empty).
	 */
	public void clearAllTiles() {
		for(int x = 0; x < XtileCount; x++) {
			for(int y = 0; y < YtileCount; y++) {
				grid[x][y].setPaint(0);
				grid[x][y].setActive(false);
			}
		}
	}

	/**
	 * Constructs all tiles as cleared.
	 */
	public void genTiles() {
		for(int x = 0; x < XtileCount; x++) {
			for(int y = 0; y < YtileCount; y++) {
				grid[x][y] = new Tile(0, false);
			}
		}
	}

	/**
	 * Will set the paint values of the bottom line to 0, then move all tile data down.
	 */
	public void clearLine() {
		for(int x = 0; x < XtileCount; x++) {
			grid[x][YtileCount].setPaint(0);
		}
		for(int y = YtileCount - 1; y > 0; y--) {
			for(int x = 0; x < XtileCount; x++) {
				grid[x][y].move(grid[x][y + 1]);
			}
		}
	}

	//ASK PRIME why no override
	//sets values for tile mapping
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		//sets # of tiles to be drawn across & down
		XtileCount = (int) Math.floor(w / tileSize);
		YtileCount = (int) Math.floor(h / tileSize);
		//calculates extra space & splits it evenly
		XscreenOffset = (w - (tileSize * XtileCount)) / 2;
		YscreenOffset = (h - (tileSize * YtileCount)) / 2;
		//sets size of tile grid
		grid = new Tile[XtileCount][YtileCount];
	}

	//draw!
	//ASK PRIME why no override
	public void OnDraw(Canvas c) {
		for(int x = 0; x < XtileCount; x++) {
			for(int y = 0; y < YtileCount; y++) {
				tilePaint.setColor(grid[x][y].getPaint());
				c.drawRect((float)(XscreenOffset + x * XtileCount), (float)(YscreenOffset + y * YtileCount), (float)((XscreenOffset + x * XtileCount) + 10), (float)((YscreenOffset + y * YtileCount) + 10), tilePaint);
			}
		}
	}

}
